/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools;

import embeddedpaint.EmbeddedPaint;
import embeddedpaint.model.feature.AImageTool;
import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JToggleButton;


public class ToolsMovePicture extends AImageTool implements MouseMotionListener, MouseListener
{

    private static final String M_LABEL_TEXT = "Move Layer";
    private static final String M_ICON_PATH  = "images/move.png";
    
    private Point               m_from;

    public ToolsMovePicture (LayerManager imageManager, JFrame application)
    {
        super(imageManager, application);
    }

    @Override
    public boolean isToggleButton ()
    {
        return true;
    }

    @Override
    public String getLabel ()
    {
        return M_LABEL_TEXT;
    }

    @Override
    protected String getIconName ()
    {
        return M_ICON_PATH;
    }

    @Override
    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource() instanceof JToggleButton)
        {
            JToggleButton buton = (JToggleButton) e.getSource();

            if (buton.isSelected())
            {
                createObserver();
            }
            else
            {
                removeObservers();
            }
        }
    }

    public void createObserver ()
    {
        ((EmbeddedPaint) this.m_application).getM_scrollPane().addMouseListener      (this);
        ((EmbeddedPaint) this.m_application).getM_scrollPane().addMouseMotionListener(this);
    }

    public void removeObservers ()
    {
        ((EmbeddedPaint) this.m_application).getM_scrollPane().removeMouseListener      (this);
        ((EmbeddedPaint) this.m_application).getM_scrollPane().removeMouseMotionListener(this);
    }

    @Override
    public void dispose ()
    {
        removeObservers();
    }

    @Override
    public void mouseDragged (MouseEvent e)
    {
        Point newPoint = e.getPoint();

        int newX = newPoint.x - this.m_from.x;
        int newY = newPoint.y - this.m_from.y;

        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();

        for (ALayer img : selectedImages)
        {
            img.move(newX, newY);
        }
        this.m_from = newPoint;
    }

    @Override
    public void mouseMoved (MouseEvent e)
    {
    }

    @Override
    public void mouseClicked (MouseEvent e)
    {
    }

    @Override
    public void mousePressed (MouseEvent e)
    {
        this.m_from = e.getPoint();
    }

    @Override
    public void mouseReleased (MouseEvent e)
    {
    }

    @Override
    public void mouseEntered (MouseEvent e)
    {
    }

    @Override
    public void mouseExited (MouseEvent e)
    {
    }
}
